import axios from 'axios';
const API_BASE_URL = process.env.BACKEND_URL;
class Food {
constructor(token) {
this.token = token;
}
async getNutData(filter) {
let query = `${API_BASE_URL}/api/NutData/`;
if (filter) {
query += `?filter=${JSON.stringify(filter)}&access_token=${this.token}`;
} else {
query += `?access_token=${this.token}`;
}
const res = await axios.get(query);
return res;
}
async getRelatedNutritonSource(id) {
let query = `${API_BASE_URL}/api/NutData/`;
if (id && parseInt(id, 10)) {
query += `${id}/nutDataDataSrc?access_token=${this.token}`;
}
const res = await axios.get(query);
return res;
}
async getRelatedFoods(id) {
let query = `${API_BASE_URL}/api/NutData/`;
if (id && parseInt(id, 10)) {
query += `${id}/nutDataFoods?access_token=${this.token}`;
}
const res = await axios.get(query);
return res;
}
async getRelatedSourceCd(id) {
let query = `${API_BASE_URL}/api/NutData/`;
if (id && parseInt(id, 10)) {
query += `${id}/nutDataSrcCd?access_token=${this.token}`;
}
const res = await axios.get(query);
return res;
}
async getRelatedNutrDef(id) {
let query = `${API_BASE_URL}/api/NutData/`;
if (id && parseInt(id, 10)) {
query += `${id}/nutDataNutrDef?access_token=${this.token}`;
}
const res = await axios.get(query);
return res;
}
async updateNutData(id, updates) {
if (!id) {
return Promise.reject(new Error('must have id send into patchNutData()'));
}
if (Object.keys(updates) === undefined || Object.keys(updates).length < 1) {
return Promise.reject(new Error('must have object with some keys that will be updated. If meant to delete use deleteNutData()'));
}
const uri = `${API_BASE_URL}/api/NutData/${id}?access_token=${this.token}`;
const res = await axios.patch(uri, updates).catch((err) => Promise.reject(err));
return res;
}
async deleteNutData(id) {
if (!id) {
return Promise.reject(new Error('must have id to be able to delete'));
}
const uri = `${API_BASE_URL}/api/NutData/${id}?access_token=${this.token}`;
const res = await axios.delete(uri).catch((err) => Promise.reject(err));
return res;
}
async createNutData(params, createBlank) {
if (!params && !createBlank) {
return Promise.reject(new Error('createBlank was false and no params were sent in, invalid config'));
}
const uri = `${API_BASE_URL}/api/NutData/?access_token=${this.token}`;
if (createBlank && !params) {
const res = await axios.post(uri).catch((err) => Promise.reject(err));
return res;
}